package com.imooc.controller;

import com.imooc.pojo.Items;
import com.imooc.pojo.ItemsImg;
import com.imooc.pojo.ItemsParam;
import com.imooc.pojo.ItemsSpec;
import com.imooc.service.ItemsService;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.PagedGridResult;
import com.imooc.vo.CommentsLevelCountsVo;
import com.imooc.vo.ItemParamsVo;
import com.imooc.vo.ShopCartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author wangke
 * @Description TODO
 * @Date 2021/7/15 13:01
 * @Version 1.0
 */
@RestController
@RequestMapping("/items")
@Api(value="商品信息接口",tags = "商品详细信息的接口")
public class ItemsController extends BaseController {
    @Autowired
    private ItemsService itemsService;

   @GetMapping("/info/{itemId}")
   @ApiOperation(value = "商品详细信息的",notes = "获取商品详细信息的接口")
    public IMOOCJSONResult getItemsInfo(@ApiParam(name="itemId",value = "商品id",required = true) @PathVariable String itemId){
       Items item = itemsService.getItem(itemId);
       ItemsParam itemParams = itemsService.getItemParams(itemId);
       List<ItemsSpec> itemSpecList = itemsService.getItemSpecList(itemId);
       List<ItemsImg> itemImgList = itemsService.getItemImgList(itemId);
       ItemParamsVo itemParamsVo = new ItemParamsVo();
       itemParamsVo.setItem(item);
       itemParamsVo.setItemImgList(itemImgList);
       itemParamsVo.setItemSpecList(itemSpecList);
       itemParamsVo.setItemParams(itemParams);

       return IMOOCJSONResult.ok(itemParamsVo);
   }
   @GetMapping("/commentLevel")
   @ApiOperation(value = "评价数量",notes = "商品各种评价数量",httpMethod = "GET")
   public IMOOCJSONResult queryCommentsLevelCount(@RequestParam String itemId){
      CommentsLevelCountsVo commentsLevelCountsVo = itemsService.queryCommentLevelCounts(itemId);
      return IMOOCJSONResult.ok(commentsLevelCountsVo);
   }

  //商品评价分页详情
   @GetMapping("/comments")
   public IMOOCJSONResult queryPagerItemsComments(@ApiParam(name = "itemId",value = "商品id",required = true)@RequestParam String itemId,
                                                  @ApiParam(name="level",value = "评价等级",required = false)@RequestParam Integer level,
                                                  @ApiParam(name="page",value = "当前页数",required = false)@RequestParam Integer page,
                                                  @ApiParam(name="pageSize",value = "每页条数",required = false) @RequestParam Integer pageSize){

      if(page==null)
         page=1;  //默认第一页
      if(pageSize==null)
         pageSize=PAGE_SIZE; //默认每页条数 10
      PagedGridResult pagedGridResult = itemsService.queryPagedComments(itemId, level, page, pageSize);
      return IMOOCJSONResult.ok(pagedGridResult);
   }

   @GetMapping("/search")
   @ApiOperation(value = "商品查询",notes = "商品的条件查询")
   public IMOOCJSONResult queryByConditionItems(@RequestParam("keywords") @ApiParam(name="keywords",value = "搜索关键词") String keywords,
                                                @ApiParam(name="sort",value = "排序关键词")@RequestParam("sort") String sort,
                                               @ApiParam(name="page",value = "当前页") @RequestParam("page") Integer page,
                                               @ApiParam(name="pageSize",value = "分页大小") @RequestParam("pageSize") Integer pageSize){
    if(StringUtils.isBlank(keywords)){
        return IMOOCJSONResult.errorMsg("搜索关键词不能为空");
    }
    if(page==null)
        page=1;  //默认第一页
    if(pageSize==null)
        pageSize=PAGE_SIZE; //默认每页条数 10
       PagedGridResult pagedGridResult = itemsService.queryConditionItems(keywords, sort, page, pageSize);
       return IMOOCJSONResult.ok(pagedGridResult);
   }
    @GetMapping("/catItems")
    @ApiOperation(value = "商品查询",notes = "商品的条件查询")
    public IMOOCJSONResult searchItemsByCategory(@RequestParam("catId") @ApiParam(name="catId",value = "分类id") Integer catId,
                                                 @ApiParam(name="sort",value = "排序关键词")@RequestParam("sort") String sort,
                                                 @ApiParam(name="page",value = "当前页") @RequestParam("page") Integer page,
                                                 @ApiParam(name="pageSize",value = "分页大小") @RequestParam("pageSize") Integer pageSize){
        if(catId==null){
            return IMOOCJSONResult.errorMsg("分类选择不能为空");
        }
        if(page==null)
            page=1;  //默认第一页
        if(pageSize==null)
            pageSize=PAGE_SIZE; //默认每页条数 10
        PagedGridResult pagedGridResult = itemsService.searchItemsByCategory(catId, sort, page, pageSize);
        return IMOOCJSONResult.ok(pagedGridResult);
    }

    //商品刷新参数
    @GetMapping("/refresh")
    @ApiOperation(value = "购物车商品规格数据刷新",notes = "购物车商品规格数据刷新",httpMethod = "GET")
    public IMOOCJSONResult queryItemsBySpec(@ApiParam(name = "itemSpecIds",value = "规格id拼接的字符串",example = "1,3,5") @RequestParam(value = "itemSpecIds") String itemSpecIds){
        if(StringUtils.isBlank(itemSpecIds)){
            //如果id有问题不处理直接返回
            return IMOOCJSONResult.ok();
        }
        List<ShopCartVo> shopCartVos = itemsService.queryItemsBySpec(itemSpecIds);

        return IMOOCJSONResult.ok(shopCartVos);
    }

}
